﻿using System;
using System.Reflection;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using TestDoc;

namespace When_Using_MicosoftTest
{
    /// <summary>
    /// Summary description for MicrosoftTestFramework
    /// </summary>
    [TestClass]
    public class TestDocsLibrary
    {
        private static Assembly _assembly;

        public TestDocsLibrary()
        {
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize()]
        public static void BeforeMsSuite(TestContext testContext)
        {
            _assembly = Assembly.GetExecutingAssembly();
        }
        
        // Use ClassCleanup to run code after all tests in a class have run
        [ClassCleanup()]
        public static void AfterMsSuite()
        {
            
        }
        
        // Use TestInitialize to run code before running each test 
        [TestInitialize()]
        public void BeforeEachMsTest()
        {
            
        }
        
        // Use TestCleanup to run code after each test has run
        [TestCleanup()]
        public void AfterEachMsTest()
        {
            
        }
        
        #endregion

        [TestMethod]
        public void ShouldRecogniseMsTests()
        {
            var testParser = new TestParser(_assembly);
            IList<MethodInfo[]> methods = testParser.GetTests();
            IList<MethodInfoSmall> output = testParser.FormatTestReport(methods);

            Assert.IsTrue(output.Contains(new MethodInfoSmall(MethodType.MethodName,"Should Recognise Ms Tests")));
        }

        [TestMethod]
        public void ShouldOnlyRecogniseMethodsWithTestAttributes()
        {
            var testParser = new TestParser(_assembly);
            IList<MethodInfo[]> methods = testParser.GetTests();
            IList<MethodInfoSmall> output = testParser.FormatTestReport(methods);

            Assert.IsFalse(output.Contains(new MethodInfoSmall(MethodType.MethodName, "Not A Test")));
                
        }

        [TestMethod]
        public void ShouldRecogniseNamespaceAndClass()
        {
            var testParser = new TestParser(_assembly);
            IList<MethodInfo[]> methods = testParser.GetTests();
            IList<MethodInfoSmall> output = testParser.FormatTestReport(methods);

            Assert.IsTrue(output.Contains(new MethodInfoSmall(MethodType.Namespace, "When Using Micosoft Test")));
            Assert.IsTrue(output.Contains(new MethodInfoSmall(MethodType.ClassName,"Test Docs Library")));

            
        }

        
        [MyAttribute]
        public void NotATest()
        {
        }

        /// <summary>
        /// Create a custom attribute to ensure we don't bring back methods with attributes but not test attributes.
        /// </summary>
        public class MyAttribute : Attribute
        {
            
        } 
    }
}
